const express = require('express');
const router = express.Router();
const topicDao = require('../database/mysql/topicDao');
const userDao = require('../database/redis/userDao');
const writeEJS = require('./writeEJS');
const topic_commentDao = require('../database/mysql/topic_commentDao');
const User = require('../database/redis/userDao');
const myMulter = require('../lib/middleware/myMulter');
const verify = require('../lib/controller/verify');


router.get('/', async function (req, res, next) {
    await writeEJS.asyncWriteHeader(req,res);

    let topicId = req.query.id || null;

    //没有参数或者参数不是数字就会到首页
    if (topicId == null || isNaN(topicId)) {
        res.redirect('/');
        return
    }

    //没有参数或者参数不是数字就会到首页
    let queryPath = req.query.path || 1;
    if (queryPath == null || isNaN(queryPath)) {
        res.redirect('/');
        return
    }
    if (queryPath < 1) {
        queryPath = 1;
    }

    //没有这个条目就重定向
    let topic = await topicDao.getEntriesDataById(topicId);
    if (topic == false) {
        res.contentType('html');
        res.end('找不到topic');
        return
    }

    //是否显示订阅按钮
    let isShowSubscrbe = false;
    if (await userDao.asyncIsSubscribeTopic(req.session.userId, topicId)) {
        isShowSubscrbe = true;
    }

    let SplitTobicComment = await topic_commentDao.getSplitTopicComment(queryPath, topicId);
    let topic_comment = SplitTobicComment.topicComment;


    //把用户头像和名字写入object
    for (let i in topic_comment) {
        let userId = topic_comment[i].user_id;
        topic_comment[i]['image'] = await User.asyncGetAvatarById(userId);
        topic_comment[i]['account'] = await User.asyncGetaccountById(userId);
    }

    res.locals.topicId = topicId;
    res.locals.isShowSubscrbe = isShowSubscrbe;

    res.locals.pathNum = SplitTobicComment.pathNum;
    res.locals.exitsToicNum = SplitTobicComment.exitsToicNum;
    res.locals.currentPathNum = SplitTobicComment.currentPathNum;
    res.locals.topicComment = topic_comment;
    res.locals.topicData = topic;
    res.render('show_topic');
})

router.get('/subscribe', async function (req, res, next) {
    //没登录就重定义
    if (req.session.userId == null) {
        res.redirect('/login');
        return
    }

    let topicId = req.query.topicId;
    let result = await userDao.asyncAddSubscribe(req.session.userId, topicId);
    if (result) {
        await topicDao.addSubscribeNumber(topicId);
    }
    res.redirect(`/show_topic?id=${topicId}`);
})

router.get('/unsubscribe', async function (req, res, next) {
    //没登录就重定义
    if (req.session.userId == null) {
        res.redirect('/login');
        return
    }

    let topicId = req.query.topicId;
    let result = await userDao.sayncDeleteSubscribe(req.session.userId, topicId);
    if (result) {
        await topicDao.reduceSubscibeNumber(topicId);
    }
    res.redirect(`/show_topic?id=${topicId}`);
})

//接收表单。。。
router.get('/add_comment', async function (req, res, next) {
    await writeEJS.asyncWriteHeader(req,res);
    let topicId = req.query.id || null;

    //没有参数,参数不是数字,没有登录就会到首页
    if (topicId == null || isNaN(topicId) || !req.session.userId) {
        res.redirect('/login');
        return
    }

    res.locals.topicId = topicId;
    res.render(`add_topic_comment`);
    //res.redirect(`add_topic_comment?id=${topicId}`)
})

router.post('/add_comment', myMulter, async function (req, res, next) {
    
    let topicId = req.query.id || null;

    //没有参数或者参数不是数字就会到首页
    if (topicId == null || isNaN(topicId)) {
        res.redirect('/');
        return
    }

    let title = req.body.title;
    let content = req.body.content;

    let status = verify.topicCommentVerify(req, title, content);
    console.log(status);
    if (status == true) {
        await topic_commentDao.insertTopic_comment(topicId, req.session.userId, `${content}`);
        res.redirect(`/show_topic?id=${topicId}`);
    } else {
        res.redirect(`/show_topic/add_comment?id=${topicId}`);
    }
})


module.exports = router;